<template>
    <div class="order-tab">
        <nut-tabs v-model="tab" size="normal" title-scroll title-gutter="10" name="tab" @click="changeTab">
            <nut-tab-pane v-for="item in tabList" :key="item.key" :pane-key="item.key" :title="item.title">
                <div class="order-list" v-for="ele in orderList">
                    <block v-if="ele != null">
                        <div class="order-item" v-for="(itemVal, index) in ele"
                            @click="jumpTo('/packageA/pages/serOrderList/details?id=' + itemVal.ID)">
                            <div class="order-no flex-a flex-b">
                                <div>订单编号：{{ itemVal.order_No }}</div>
                                <div>{{ itemVal.status == '0' ? '待付款' : itemVal.status == '1' ? '已付款' : itemVal.status
                                    ==
                                    '3' ? '待服务'
                                    : itemVal.status == '4' ? '进行中' : itemVal.status == '5' ? '已完成' :
                                        itemVal.status == '6' ? '待评价' : itemVal.status == '-1' ? '待退款'
                                            : itemVal.status == '-2' ? '已退款' : itemVal.status == '-3' ? '已取消' : '' }}
                                </div>
                            </div>
                            <div class="order-head flex-a flex-b">
                                <div class="left">
                                    <img :src="itemVal.pageSetting.remark" alt="">
                                </div>
                                <div class="right">
                                    <div class="title flex-a flex-b">
                                        <div>{{ itemVal.pageSetting.project_Name }}</div>
                                        <div><text>实付</text>￥{{ itemVal.service_Amount }}</div>
                                    </div>
                                    <div class="spec flex-a flex-b">
                                        <div>{{ itemVal.pageSettingSec.project_Name }}</div>
                                        <div></div>
                                        <!-- <div>{{ 'x' + itemVal.count }}</div> -->
                                    </div>
                                </div>
                            </div>
                            <div class="time flex-a">
                                <div>服务时长：{{ itemVal.count * 60 }}分钟</div>
                                <div>预约时间：{{ itemVal.service_Time }}</div>
                            </div>
                            <div class="order-foot flex-a">
                                <nut-button color="#F0A93F" size="small" style="border-radius: 10rpx;"
                                    v-if="ele.length == 1 && itemVal.pageSetting.category == '1'"
                                    @click.stop="changePrice(itemVal)">改价</nut-button>
                                <nut-button color="#21C38F" size="small" v-if="itemVal.status == '1'"
                                    @click.stop="receOrder(itemVal)">接收订单</nut-button>
                                <nut-button color="#21C38F" size="small" v-if="itemVal.status == '3'"
                                    @click.stop="startOrder(itemVal)">开始服务</nut-button>
                                <nut-button color="#21C38F" size="small" v-if="itemVal.status == '4'"
                                    @click.stop="endOrder(itemVal)">结束服务</nut-button>
                                <nut-button plain color="#FE6F49" size="small" v-if="itemVal.status == '1'"
                                    @click.stop="resetOrder(itemVal)">拒绝订单</nut-button>
                            </div>
                        </div>
                    </block>
                </div>
                <nut-empty description="暂无数据" v-if="orderList.length == 0"></nut-empty>
            </nut-tab-pane>
        </nut-tabs>
        <nut-dialog title="改价" v-model:visible="visible" @cancel="onCancel" @ok="onOk">
            <nut-form label-position="top" star-position="right">
                <nut-form-item label="最新价格：" required label-position="top" star-position="left">
                    <nut-input style="border: 1rpx solid #ccc;border-radius: 10rpx;padding: 10rpx;"
                        placeholder="请输入最新价格" v-model="price" type="text" />
                </nut-form-item>
            </nut-form>
        </nut-dialog>
    </div>
</template>

<script lang="ts">
import Taro, { useRouter } from '@tarojs/taro';
import { useReachBottom } from '@tarojs/taro';
import { Left, MoreX } from '@nutui/icons-vue-taro';
import { reactive, toRefs, onMounted } from 'vue';
import { getOrderList, pay, changeOrder, againOrder } from '../../../../api/index'
export default {
    components: {
        Left,
        MoreX
    },
    setup() {
        const data = reactive({
            imgUrl:
                'https://cxj.szzkba.cn/upload/i/2024/06/04/182133.jpeg',
            visible: false,
            currDate: {},
            price: '',
            waybillToken: '',
            tab: 'null' as any,
            orderList: [
                // {
                //     orderNo: '354612348945612',
                //     status: 1,
                //     url: 'https://cxj.szzkba.cn/upload/i/2024/07/23/141815.png',
                //     title: '上门维修水龙头',
                //     time: '2021-09-19 10:00',
                //     less: '104',
                //     price: '99.9',
                // }
            ] as any[],
            tabList: [
                {
                    title: '全部',
                    key: 'null',
                },
                {
                    title: '待接单',
                    key: 1,
                },
                {
                    title: '待服务',
                    key: 3,
                },
                {
                    title: '进行中',
                    key: 4,
                },
                {
                    title: '已完成',
                    key: 5
                },
            ],
            page: 1,
            text: 'DPK364116621912',
            showBottom: false,
            refOrderNo: '',
            tkNum: '',
            token: '',
            user: {}
        });

        onMounted(() => {
            const router = useRouter();
            if (router.params.tab) {
                data.tab = router.params.tab
            }
            Taro.getStorage({
                key: 'user',
                success: function (res) {
                    data.user = res.data ? res.data : {}
                    getOrder()
                }
            })
        })
        const changeTab = (title) => {
            data.tab = title.paneKey
            data.page = 1
            data.orderList = []
            getOrder()
        }
        const getOrder = () => {
            getOrderList({ server_User_Id: data.user.uuid, page: data.page, pageSize: 10, status: data.tab }).then(res => {
                if (res.code == 0) {
                    let list = res.data.list, arr = [] as any
                    list.forEach(item => {
                        if (item != null) {
                            item.forEach(ele => {
                                ele.CreatedAt = reTime(ele.CreatedAt)
                            })
                        } else {

                        }
                    });
                    list.forEach(item => {
                        if (item != null) {
                            arr.push(item)
                        }
                    })
                    data.orderList = [...data.orderList, ...arr]
                    console.log(data.orderList)
                }
            })
        }
        const resetOrder = (item) => {
            changeOrder({ order_No: item.order_No, status: -3 }).then(res => {
                if (res.code == 0) {
                    jumpTo('/packageA/pages/serOrderList/index?tab=' + data.tab)
                }
            })
        }
        const receOrder = (item) => {
            changeOrder({ order_No: item.order_No, status: 3 }).then(res => {
                if (res.code == 0) {
                    jumpTo('/packageA/pages/serOrderList/index?tab=' + data.tab)
                }
            })
        }
        const startOrder = (item) => {
            Taro.getLocation({
                type: 'wgs84',
                success: function (res) {
                    const latitude = res.latitude
                    const longitude = res.longitude
                    const speed = res.speed
                    const accuracy = res.accuracy
                    console.log(res)//需要做判断定位100米内可以开始服务
                    console.log(item)
                }
            })
            changeOrder({ order_No: item.order_No, status: 4 }).then(res => {
                if (res.code == 0) {
                    jumpTo('/packageA/pages/serOrderList/index?tab=' + data.tab)
                }
            })
        }
        const endOrder = (item) => {
            changeOrder({ order_No: item.order_No, status: 5 }).then(res => {
                if (res.code == 0) {
                    jumpTo('/packageA/pages/serOrderList/index?tab=' + data.tab)
                }
            })
        }
        const addZero = (n) => {
            return n < 10 ? '0' + n : n
        }
        const reTime = (t) => {
            let time = new Date(t)
            let y = time.getFullYear()
            let m = addZero(time.getMonth() + 1)
            let d = addZero(time.getDate())
            let h = addZero(time.getHours())
            let f = addZero(time.getMinutes())
            let s = addZero(time.getSeconds())
            return y + '-' + m + '-' + d + ' ' + h + ':' + f + ':' + s
        }
        // const toPay = (item) => {
        //     pay({
        //         order_No: item.order_No,
        //         openId: data.user.openId,
        //         title: '支付调用',
        //         remark: '',
        //     }).then(res => {
        //         if (res.code == 0) {
        //             Taro.requestPayment({
        //                 timeStamp: res.data.timeStamp,
        //                 nonceStr: res.data.nonceStr,
        //                 package: res.data.package,
        //                 signType: res.data.signType,
        //                 paySign: res.data.paySign,
        //                 success: function (res) {
        //                     getOrder(data.tab)
        //                 },
        //             })
        //         }
        //     })
        // }
        const jumpTo = (path) => {
            Taro.navigateTo({ url: path });
        }
        const changePrice = (item) => {
            data.currDate = item
            data.visible = true
        }
        const onCancel = () => {
            data.visible = false
        }
        const onOk = () => {
            if (data.price != '') {
                let params = {
                    server_id: data.currDate.server_id,
                    server_User_Id: data.currDate.server_User_Id,
                    user_ID: data.currDate.user_ID,
                    service_Name: data.currDate.service_Name,
                    service_Time: data.currDate.service_Time,
                    service_Addr: data.currDate.service_Addr,
                    service_Lng: data.currDate.service_Lng,
                    service_Lat: data.currDate.service_Lat,
                    service_Amount: Number(data.price),
                    client_ID: data.currDate.client_ID,
                    taxi_Amount: 0,
                    Count: data.currDate.count,
                    orderAllNo: data.currDate.orderAllNo
                }
                againOrder(params).then(res => {
                    if (res.code == 0) {
                        Taro.showToast({
                            title: '提交成功',
                            icon: 'success',
                            duration: 2000
                        })
                        getOrder()
                    }
                })
            } else {
                Taro.showToast({
                    title: '价格必填',
                    icon: 'error',
                    duration: 2000
                })
            }
        }
        useReachBottom(() => {
            //console.log('onReachBottom')
            data.page++
            getOrder()
        })
        return {
            ...toRefs(data), changePrice, onCancel, onOk, changeTab, jumpTo, getOrder, reTime, addZero, resetOrder, receOrder, startOrder, endOrder
        }
    }
};
</script>
<style lang="scss">
.order-tab {
    .nut-tabs__titles-item__line {
        background: #F0A93F !important;
        width: 30rpx !important;
    }

    .nut-tab-pane {
        padding: 0;
    }

    .order-list {
        padding: 20rpx 0 0;
        background: #E9E9E9;

        .order-item {
            background: #fff;
            box-sizing: border-box;
            padding: 20rpx 0;
            margin-bottom: 20rpx;

            .order-no {
                margin-bottom: 20rpx;
                padding: 0 20rpx 20rpx 20rpx;
                border-bottom: 1rpx solid #F1F1F1;

                div:first-child {
                    font-size: 26rpx;
                    color: #101234;
                    font-weight: bold;
                }

                div:last-child {
                    color: #F25330;
                    font-size: 28rpx;
                }
            }

            .order-head {
                padding: 0 20rpx 20rpx 20rpx;
            }

            .left {
                width: 15%;

                img {
                    width: 80rpx;
                    height: 80rpx;
                    border-radius: 10rpx;
                    float: left;
                }
            }

            .right {
                width: 84%;
                position: relative;

                .title {
                    margin-bottom: 10rpx;
                    color: #191B3C;

                    div:first-child {
                        font-size: 28rpx;
                        font-weight: bold;
                    }

                    div:last-child {
                        font-size: 32rpx;
                        font-weight: bold;
                        color: #070A2D;

                        text {
                            color: #999AA6;
                            margin-right: 10rpx;
                            font-weight: normal;
                        }
                    }
                }

                .spec {
                    color: #9393A1;
                    font-size: 28rpx;
                }
            }

            .time {
                color: #9899A5;
                font-size: 26rpx;
                margin: 10rpx 20rpx;

                div:first-child {
                    margin-right: 40rpx;
                }
            }

            .order-foot {
                padding: 20rpx 20rpx 0;
                justify-content: flex-end;

                .nut-button {
                    margin-left: 10rpx;
                }
            }
        }
    }
}

.tknum {
    .nut-popup {
        padding-top: 80rpx;
    }

    .btn {
        text-align: center;

        .nut-button {
            width: 60%;
            margin: 20rpx auto;
        }
    }
}
</style>